package compro.admission.form.service;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import compro.admission.form.dao.UserDAO;
import compro.admission.form.email.RegisterEmail;
import compro.admission.form.entity.User;
import compro.admission.form.util.PasswordGenerator;

@Component
@Transactional(propagation = Propagation.REQUIRED)
public class UserService {
	private UserDAO userDAO;

	public UserDAO getUserDAO() {
		return userDAO;
	}

	@Resource
	public void setUserDAO(UserDAO userDAO) {
		this.userDAO = userDAO;
	}
	
	public User getUserLogin(User user) {
		User userLogin = userDAO.getUserByEmail(user.getEmail());
		if(userLogin == null )
			return null;
		
		if(userLogin.getPassword().equals(user.getPassword()))
			return userLogin;
		else
			return null;
	}
	public User getUserAdmission() {
		User user = userDAO.getUserAdmission();
		return user;
	}

	public void saveUser(User user) {
		user.setPassword(PasswordGenerator.generatePassword());
		RegisterEmail regisEmail = new RegisterEmail(user);
		regisEmail.sendEmail();
		user.setRole("applicant");
		userDAO.saveUser(user);
	}

	public void updateUser(User user) {
		userDAO.updateUser(user);
	}

	public String checkEmail(String email) {
		User user = userDAO.getUserByEmail(email);
		if(user == null)
			return "true";
		else
			return "false";
	}
	//By sudha
	public User getUserbyemail(String email)
	{
		User user = userDAO.getUserByEmail(email);
		return user;
	}
}
